package com.catersystem.zmy.catersystem.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.activity.HomeActivity;
import com.catersystem.zmy.catersystem.base.BaseFragment;
import com.catersystem.zmy.catersystem.utils.DeviceUtils;
import com.catersystem.zmy.catersystem.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 基础配置
 * Created by Administrator on 2017/10/30 0030.
 */

public class BasicConfigurationFragment extends BaseFragment {
    @BindView(R.id.sn_type)
    Spinner mSnType;//http/https切换
    @BindView(R.id.et_service_address)
    EditText mEtServiceAddress;//服务器地址
    @BindView(R.id.sn_type2)
    Spinner mSnType2;//http/https切换
    @BindView(R.id.et_service_address2)
    EditText mEtServiceAddress2;//服务器地址
    @BindView(R.id.et_shop_num)
    EditText mEtShopNum;//门店编号
    @BindView(R.id.tv_cater_num)
    EditText mTvCaterNum;//菜品类别
    @BindView(R.id.tv_device_num)
    TextView mTvDeviceNum;//设备编号
    @BindView(R.id.tv_configuration)
    TextView mTvConfiguration;//设备编号

    private String addressType = "";
    private String addressType2 = "";
    private String[] mType;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_setting_basic, container, false);
    }

    @Override
    protected void initView(View view) {
        mTvConfiguration.setClickable(true);
        if (!TextUtils.isEmpty(SPUtils.getInstance().getString("service_address"))){
            mEtServiceAddress.setText("canyin.qcterp.com");
            mEtServiceAddress2.setText("cy_api.qcterp.com/blmticketapi");
            mEtShopNum.setText(SPUtils.getInstance().getString("orgcode"));
            mTvCaterNum.setText("123");
            mTvConfiguration.setClickable(false);
        }
        mTvDeviceNum.setText(DeviceUtils.getUniqueId(activity));//自动生成设备编码
        mType = getResources().getStringArray(R.array.type);
        mSnType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                addressType = mType[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mSnType2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                addressType2 = mType[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @OnClick({R.id.tv_configuration})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_configuration:
                addressType += "canyin.qcterp.com";
                addressType2 += "cy_api.qcterp.com/blmticketapi/";
                mEtServiceAddress.setText("canyin.qcterp.com");
                mEtServiceAddress2.setText("cy_api.qcterp.com/blmticketapi");
                mEtShopNum.setText("quanchengtao");
                mTvCaterNum.setText("123");
                SPUtils.getInstance().put("orgcode", mEtShopNum.getText().toString());
                SPUtils.getInstance().put("appguid", "e807cd996be775b69f3510fd4b742564");
                SPUtils.getInstance().put("service_address", addressType);
                SPUtils.getInstance().put("service_address2",addressType2);
                startActivityAndFinish(HomeActivity.class);
                break;
        }
    }
}
